using System.Collections.Generic;
using Firestarter.DB.Schema.Logic;

namespace Firestarter.DM_DB.Logic
{
    public class DMTypeToDBColumnTypeMapper : IDMTypeToDBColumnTypeMapper
    {
        private readonly Dictionary<string , string> _typeMappings;

        public DMTypeToDBColumnTypeMapper()
        {
            _typeMappings = new Dictionary<string, string>();
            SetupDefaultTypeMappings();
        }

        private void SetupDefaultTypeMappings()
        {
            AddTypeMapping("System", "Int32", DBDataTypes.INTEGER);
            AddTypeMapping("System", "Boolean", DBDataTypes.BIT);
            AddTypeMapping("System", "Byte[]", DBDataTypes.VARBINARY);
            AddTypeMapping("System", "DateTime", DBDataTypes.DATETIME);
            AddTypeMapping("System", "Decimal", DBDataTypes.DECIMAL);
            AddTypeMapping("System", "Double", DBDataTypes.DOUBLE);
            AddTypeMapping("System", "String", DBDataTypes.VARCHAR);
            AddTypeMapping("System", "Int16", DBDataTypes.SMALLINT);
            AddTypeMapping("System", "Int64", DBDataTypes.BIGINT);
            AddTypeMapping("System", "Byte", DBDataTypes.TINYINT);
            AddTypeMapping("System", "Guid", DBDataTypes.UNIQUEIDENTIFIER);
            AddTypeMapping("System", "Single", DBDataTypes.SINGLE);
            AddTypeMapping("System", "Char", DBDataTypes.CHAR);
            AddTypeMapping("System", "TimeSpan", DBDataTypes.TIME);
        }

        private void AddTypeMapping(string assemblyName, string typeName, string universalDataTypeName)
        {
            _typeMappings.Add(new TypeDetail(assemblyName, typeName).ToString(), universalDataTypeName);
        }
        public string GetDBColumnType(string assemblyName, string typeName)
        {
            TypeDetail typeDetail = new TypeDetail(assemblyName, typeName);
            return _typeMappings[typeDetail.ToString()];
        }

        internal class TypeDetail
        {
            public string AssemblyName { get; set; }
            public string TypeName { get; set; }

            public TypeDetail(string assemblyName, string typeName)
            {
                AssemblyName = assemblyName;
                TypeName = typeName;
            }

            public override string ToString()
            {
                return this.AssemblyName + "-" + this.TypeName;
            }
        }
    }

    public interface IDMTypeToDBColumnTypeMapper
    {
        string GetDBColumnType(string assemblyName, string typeName);
    }
}